<?php namespace app\common\service;
use \app\common\model\UserDeposit as UserDepositModel;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/23
 * Time: 17:07
 */
class UserDeposit extends Base {
    public $model;
    public function __construct()
    {
        $this->model = new UserDepositModel();

    }

    public function getMyDeposit($where,$field,$limit,$order){
        return  $this->model->where($where)->field($field)->limit($limit)->order($order)->select();
    }

    /**提现数据处理
     * @param $site
     */
    public function setUserDeposit($site,$data,$user,$uid){
//        $cash_days=$site['cash_days'];
//        $cash_rimt=$site['cash_rimt'];
//        $cash_day_max=$site['cash_day_max'];
//        $cash_week_max=$site['cash_week_max'];
//        $cash_m_max=$site['cash_m_max'];
        $cash_min=$site['cash_min'];
//        $cash_beishu=$site['cash_beishu'];
//        $cash_fee_baifenbi=$site['cash_fee_baifenbi'];
//        $cash_baifenbi=$site['cash_baifenbi'];
        $datas = json_encode((object)null);
//        if($cash_baifenbi>100){
//            $cash_baifenbi=100;
//        }
//        if($cash_fee_baifenbi>100){
//            $cash_fee_baifenbi=100;
//        }
//        if($cash_fee_baifenbi>100){
//            $this->error('暂时不能提现');
//            return ['code'=>2,'msg'=>'暂时不能提现','data'=>$datas];
//        }

//        $week=date("w");
//        $time=date('H:i');
        if(empty($data['id'])){
            return ['code'=>2,'msg'=>'必须选择提现的银行账户','data'=>$datas];
        }
        if(empty($data['money'])){
            return ['code'=>2,'msg'=>'金额不能为空','data'=>$datas];
        }
        if($data['money']<=2){
            return ['code'=>2,'msg'=>'提现金额必须大于2元','data'=>$datas];
        }
//        if(!preg_match("/^\d*$/",$data['money'])){
//            return ['code'=>2,'msg'=>'输入提现金额不合法','data'=>$datas];
//        }

//        if(!empty($cash_days)){
//            $weekP=explode('-',$cash_days);
//            if(is_array($weekP)){
//                if(!in_array($week,$weekP)){
//                    return ['code'=>2,'msg'=>'必须在星期'.$cash_days.'才能提现!','data'=>$datas];
//                }
//            }else{
//                if(!$weekP==$week){
//                    return ['code'=>2,'msg'=>'必须在星期'.$cash_days.'提现之内才能提现！','data'=>$datas];
//                }
//            }
//        }
//        if(!empty($cash_rimt)){
//            $cash_rimt_array=explode('-',$cash_rimt);
//            if(is_array($cash_rimt_array)){
//                if($time>$cash_rimt_array[1]||$time<$cash_rimt_array[0]){
//                    return ['code'=>2,'msg'=>'必须在'.$cash_rimt_array[0].'-'.$cash_rimt_array[1].'才能提现！','data'=>$datas];
//                }
//            }
//        }
//        if(!empty($cash_day_max)){
//            $t = time();
//            $start_time = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
//            $end_time = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
//            $depositWhere['createtime']=array('between',array($start_time,$end_time));
//            $depositWhere['user_id']=array('eq',$uid);
//            $cash_day_max_count=Db::name('user_deposit')->where($depositWhere)->count('user_id');
//            if($cash_day_max_count>=$cash_day_max){
//                return ['code'=>2,'msg'=>'每天最大提现数量'.$cash_day_max.'！','data'=>$datas];
//            }
//        }
//        if(!empty($cash_week_max)){
//            $sdefaultDate = date("Y-m-d");
//            $first=1;
//            $w=date('w',strtotime($sdefaultDate));
//            $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
//            $week_end=date('Y-m-d',strtotime("$week_start +6 days"));
//
//            $depositWhere['createtime']=array('between',array(strtotime($week_start),strtotime($end_time)));
//            $depositWhere['user_id']=$uid;
//            $cash_week_max_count=Db::name('user_deposit')->where($depositWhere)->count('user_id');
//            if($cash_week_max_count>=$cash_week_max){
//                return ['code'=>2,'msg'=>'每周最大提现数量'.$cash_week_max.'！','data'=>$datas];
//            }
//        }

//        if(!empty($cash_m_max)){
//
//            $y = date("Y", time()); //年
//            $m = date("m", time()); //月
//            $d = date("d", time()); //日
//            $t0 = date('t'); // 本月一共有几天
//            $start_month = mktime(0, 0, 0, $m, 1, $y);
//            $end_month = mktime(23, 59, 59, $m, $t0, $y);
//            $depositWhere['createtime']=array('between',array($start_month,$end_month));
//            $depositWhere['user_id']=array('eq',$uid);
//            $cash_m_max_count=Db::name('user_deposit')->where($depositWhere)->count('user_id');
//            if($cash_m_max_count>=$cash_m_max){
//                return ['code'=>2,'msg'=>'每月最大提现数量'.$cash_m_max.'！','data'=>$datas];
//            }
//
//        }

        if(!empty($cash_min)){
            if($data['money']<$cash_min){
                return ['code'=>2,'msg'=>'不能小于最低提现金额'.$cash_min.'！','data'=>$datas];
            }
        }
//        if(!empty($cash_baifenbi)){
//            if($data['money']>$data['money']*$cash_baifenbi){
//                return ['code'=>2,'msg'=>'提现不能大于'.$data['money']*$cash_baifenbi.'！','data'=>$datas];
//            }
//        }
//        if(!empty($cash_beishu)){
//            if($data['money']%$cash_beishu){
//                return ['code'=>2,'msg'=>'必须是提现金额倍数'.$cash_beishu.'!','data'=>$datas];
//            }
//        }
//        if(!empty($cash_baifenbi)){
//            if($data['money']>($user['money']*($cash_baifenbi/100))){
//                return ['code'=>2,'msg'=>'可提现金额占余额的百分比'.$cash_baifenbi.'!','data'=>$datas];
//            }
//        }
        Db::startTrans();
        try{

            $cost_money=$data['money'];
            $datamoney['user_id']=$uid;
            $datamoney['money']=$cost_money;
            $datamoney['memo']='提现';
            $datamoney['before']=$user['money'];
            $datamoney['after']=$user['money']-$cost_money;
            $datamoney['createtime']=time();
            $datamoney['money_type']=7;
            $moneylog_id=Db::name('user_money_log')->insertGetId($datamoney);
//            用户银行卡提现表
            $bank['id']=$data['id'];
            $bank_info = Db::name("user_bank")->where($bank)->find();
            if($bank_info){
                $_data['moneylog_id']=$moneylog_id;
                $_data['user_id']=$uid;
                $_data['banking_id']=$bank_info['banking_id'];
                $_data['account']=$bank_info['account'];//银行卡号
                $_data['name']=$user['username']; // 用户信息
                $_data['branch']=$bank_info['branch'];// 支行
                $_data['compellation']=$bank_info['compellation'];
                $_data['type']=1;
                $_data['money']=$data['money'];
                $_data['cash_fee']=0;
                $_data['mobile']=$bank_info['mobile'];
                $_data['createtime']=time();
                $_data['status']=1;
            }else{
                return ['code'=>2,'msg'=>'银行卡信息不匹配','data'=>$datas];
            }

            Db::name('user_deposit')->insert($_data);
            Db::name('user')->where(['id'=>$uid])->setDec('money',$data['money']);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return ['code'=>0,'msg'=>'申请失败','data'=>$e->getFile().$e->getMessage().$e->getLine()];
        }
        return ['code'=>1,'msg'=>'申请成功','data'=>$datas];
    }



    public function getUserDeposit($where,$limit,$page){
        return $this->model->where($where)
            ->field("createtime,compellation,money,status,user_id")
            ->limit($limit)
            ->page($page)
            ->order('id desc')
            ->select();
    }
}